package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.MenuDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.mapper.MenuMapper;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import com.itheima.health.vo.MenuVO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class MemuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

//    @Autowired
//    private RoleService roleService;
    @Override
    public Result findPage(QueryPageBean queryPageBean) {

        Page<Menu> page=null;
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (queryPageBean.getQueryString() == null) {
               page = page(new Page<Menu>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),wrapper);
        }else {
            wrapper.like("name",queryPageBean.getQueryString());
            page = page(new Page<Menu>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),wrapper);
        }
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        Result result = new Result(pageResult);
        return result;
    }

    @Override
    public Boolean add(MenuDTO menuDTO) {
//        Integer id = menuDTO.getId();
//        if (StringUtils.isNotBlank(String.valueOf(id))) {
//            //根据id删除中间表数据
//            baseMapper.deleteRoleInfoByMenuId(id);
//        }
        Boolean flag = saveOrUpdate(menuDTO);
//        Integer gid = menuDTO.getId();
//        //录入中间表
//        Integer[] roleIds = menuDTO.getRoleIds();
//        if (roleIds != null && roleIds.length > 0) {
//            //中间表循环录入
//            for (Integer roleId : roleIds){
//                baseMapper.addRoleAndMenuId(gid,roleId);
//            }
//        }
        return flag;
    }

//    @Override
//    public MenuVO findCheckItemIdsbyGroupId(int id) {
//
//         List<MenuVO> list = baseMapper.findAll();
//        int[] roleIds= baseMapper.findPermissionIdsbyRoleId(id);
//        MenuVO MenuVO = new MenuVO();
//        MenuVO.setMenuList(list);
//        MenuVO.setRoleIds(roleIds);
//        return MenuVO;

//    }

    @Override
    public Boolean deleteById(int id) {
        Menu menu = new Menu();
        menu.setId(id);
        menu.setIs_delete(1);
        boolean flag = updateById(menu);
        return flag;
    }

    @Override
    public List<Menu> findAllMenu() {
        List<Menu> list = baseMapper.findAllMenu();
        return list;
    }

    @Override
    public List<MenuVO> getMenuList(Integer id) {
        return baseMapper.getMenuList(id);
    }
}
